package com.zhr.tea.view.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhr.tea.R;
import com.zhr.tea.entity.shouye.BodyEnter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by ym on 2016/11/3.
 */

public class ShouyeCenterEnterAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    public static final int TYPE1 = 0;
    public static final int TYPE2 = 1;
    private List<BodyEnter.ContentBean> data;
    private Context context;
    private Listener listener;

    public ShouyeCenterEnterAdapter(Context context, List<BodyEnter.ContentBean> data) {
        this.context = context;
        this.data = data;
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        if (viewType == TYPE1) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_shou_center_enter, null);
            holder = new EnterViewHolder(view);
            return holder;
        } else if (viewType == TYPE2) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_shou_center_enter1, null);
            holder = new EnterHeadViewHolder(view);
            return holder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof EnterViewHolder) {
            Glide.with(context).load(data.get(0).getGoodsList().get(position - 1).getPhotoUrl()).placeholder(R.mipmap.diy_bg).into(((EnterViewHolder) holder).ivPhoto);
            ((EnterViewHolder) holder).tvName.setText(data.get(0).getGoodsList().get(position - 1).getNAME());
            ((EnterViewHolder) holder).tvPrice.setText("¥" + data.get(0).getGoodsList().get(position - 1).getPrice());
            holder.itemView.setTag(data.get(0).getGoodsList().get(position - 1).getGoodsId());
            holder.itemView.setOnClickListener(this);
        } else if (holder instanceof EnterHeadViewHolder) {

            ((EnterHeadViewHolder) holder).ivHeadPhoto.setVisibility(View.GONE);

        }
    }


    @Override
    public int getItemCount() {
        return data.get(0).getHeadPhoto() != null ? data.get(0).getGoodsList().size() + 1 : data.get(0).getGoodsList().size();
    }

    @Override
    public void onClick(View view) {
        listener.ItemClick(view, (Integer) view.getTag());
    }

    public interface Listener {
        void ItemClick(View view, int code);
    }

    public class EnterViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_photo)
        ImageView ivPhoto;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_price)
        TextView tvPrice;

        public EnterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE2;
        } else {
            return TYPE1;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        GridLayoutManager manager = (GridLayoutManager) recyclerView.getLayoutManager();
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) {
                    return 2;
                } else {
                    return 1;
                }
            }
        });
    }

    public class EnterHeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_headphoto)
        ImageView ivHeadPhoto;


        public EnterHeadViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


}
